package models.security;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.Table;

import framework.base.BaseModel;
import framework.security.models.SecurityMenu;
import framework.utils.StringUtil;
import models.security.utils.SecurityConstant;

@Entity
@Table(name="T_RESOURCE")
public class Resource extends BaseModel {

   public String name;
   
   public String code;
   
   public String url;
   
   public String category;
   
   public String isMenu;
   
  
   
   public Resource(){
       super();
   }
   
   public  Resource(String name,String code,String url,String category){
       this.name = name;
       this.code = code;
       this.url = url;
       this.category = category;
       this.status = USE_STATUS ;
   }
   public  Resource(String name,String code,String url,String category,String isMenu){
       this.name = name;
       this.code = code;
       this.url = url;
       this.category = category;
       this.status = USE_STATUS;
       this.isMenu = isMenu;
   }
   /**
    * 查询所有的资源
    * @return
    */
   public static List<Resource> queryResources(){
       String sql ="from Resource a where a.status=? order by a.code";
       return Resource.find(sql, USE_STATUS).fetch();
   }
   
   /**
    * 资源字典
    * @return
    */
   public static Map<String,String> resourcesDic(){
       Map<String,String> result = new HashMap<String,String>();
       List<Resource> dataList = Resource.findAll();
       for(Resource r: dataList){
           result.put(r.code, r.name);
       }
       return result;
   }
   
   /**
    * 根据资源的类型查询所有的资源信息
    * @param type
    * @return
    */
   public static List<Resource> queryByType(String category){
       String sql ="from Resource a where a.status=? and a.category=? order by a.code asc";
       return Resource.find(sql, USE_STATUS,category).fetch();
   }
   
   /**
    * 是否是根节点资源
    * @return
    */
   public boolean isRoot(){
       return this.code.length() ==3;
   }
   
   /**
    * 获取父亲节点编号
    * @return
    */
   public  String rootCode(){
       String result = this.code;
       if(!this.isRoot()){
           int len = this.code.length();
           result = result.substring(0,len-3); 
       }
       return result;
   }
   
   /**
    * 查询所有的菜单项
    * @return
    */
   public static Map<Resource,List<Resource>> queryResourcesForMenu(){
       Map<Resource,List<Resource>> result = new LinkedHashMap<Resource,List<Resource>>();
       Map<String,Resource> roots = new LinkedHashMap<String,Resource>();
       Map<String,List<Resource>> children = new HashMap<String,List<Resource>>();
       List<Resource>  allResources = queryByType(SecurityConstant.RESOURCE_MENU_TYPE_NAME);
       List<Resource> nodes = null;
       for(Resource r : allResources){
           String rootCode = r.rootCode();
           if(r.code.equals(rootCode)){
               roots.put(rootCode,r);
               children.put(rootCode, new ArrayList<Resource>());
           }else{
               nodes = children.get(rootCode);
               if(null!=nodes){
                   nodes.add(r);
                   children.put(rootCode, nodes);
               }
           }
       }
       for(Entry<String,Resource> entry : roots.entrySet()){
           result.put(entry.getValue(),children.get(entry.getKey()));
       }
       return result;
   }
   
   /**
    * 所有的资源编码
    * @return
    */
   public static Set<String> allResourceCodes(){
       List<Resource> resources = Resource.findAll();
       Set<String> result = new HashSet<String>();
       for(Resource r : resources){
           result.add(r.code);
       }
       return result;
   }
   
   
   /**
    * 转换为菜单对象
    * @return
    */
   public SecurityMenu parseToSecurityMenu(){
       boolean _isMenu = true;
       if("false".equals(this.isMenu)){
           _isMenu = false;
       }
       SecurityMenu menu = new SecurityMenu(this.id+"",StringUtil.trim(this.url),StringUtil.trim(this.name),StringUtil.trim(this.code),_isMenu);
       return menu;
   }
   
   /**
    * 生成菜单对象
    * @return
    */
   public static Map<SecurityMenu,List<SecurityMenu>> querySecurityMenus(){
       Map<Resource,List<Resource>> resources = queryResourcesForMenu();
       Map<SecurityMenu,List<SecurityMenu>> menus = new LinkedHashMap<SecurityMenu,List<SecurityMenu>>();
       for(Entry<Resource,List<Resource>> resource : resources.entrySet()){
           List<SecurityMenu> children = new ArrayList<SecurityMenu>();
           for(Resource r : resource.getValue()){
               children.add(r.parseToSecurityMenu());
           }
           menus.put(resource.getKey().parseToSecurityMenu(), children);
       }
       return menus;
   }
}

